<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>GrPPI: pool.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo.svg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">GrPPI
   &#160;<span id="projectnumber">0.2</span>
   </div>
   <div id="projectbrief">Generic and Reusable Parallel Pattern Interface</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_d44c64559bbebec7f509842c48db8b23.html">include</a></li><li class="navelem"><a class="el" href="dir_b519bdaee6a7f8dbbe55c1237cf8f544.html">native</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">pool.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="pool_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="preprocessor">#ifndef GRPPI_NATIVE_POOL_H</span></div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;<span class="preprocessor">#define GRPPI_NATIVE_POOL_H</span></div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;<span class="preprocessor">#include &lt;boost/asio/io_service.hpp&gt;</span></div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;<span class="preprocessor">#include &lt;boost/bind.hpp&gt;</span></div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;<span class="preprocessor">#include &lt;boost/thread/thread.hpp&gt;</span></div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespacegrppi.html">grppi</a> {</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;</div><div class="line"><a name="l00030"></a><span class="lineno"><a class="line" href="classgrppi_1_1thread__pool.html">   30</a></span>&#160;<span class="keyword">class </span><a class="code" href="classgrppi_1_1thread__pool.html">thread_pool</a></div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;{</div><div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;  <span class="keyword">private</span>: </div><div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;    boost::thread_group threadpool;</div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;    boost::asio::io_service ioService;</div><div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;    std::vector&lt;boost::asio::io_service::work&gt; works;<span class="comment">//(ioService);</span></div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;    <span class="keywordtype">int</span> busy_threads= 0;</div><div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;</div><div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;  <span class="keyword">public</span>:</div><div class="line"><a name="l00039"></a><span class="lineno"><a class="line" href="classgrppi_1_1thread__pool.html#a53583fb17ac9cbbe4c5d81507265c145">   39</a></span>&#160;    <a class="code" href="classgrppi_1_1thread__pool.html#a53583fb17ac9cbbe4c5d81507265c145">thread_pool</a> (){</div><div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;    };</div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;    </div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;    <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; </div><div class="line"><a name="l00043"></a><span class="lineno"><a class="line" href="classgrppi_1_1thread__pool.html#a81730d72e5971e321618386e560f1cd1">   43</a></span>&#160;     <span class="keywordtype">void</span> <a class="code" href="classgrppi_1_1thread__pool.html#a81730d72e5971e321618386e560f1cd1">create_task</a>(T task ) { ioService.post(task); }</div><div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;</div><div class="line"><a name="l00045"></a><span class="lineno"><a class="line" href="classgrppi_1_1thread__pool.html#a0b4a992459c09e7cc72f0a06df531d53">   45</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="classgrppi_1_1thread__pool.html#a0b4a992459c09e7cc72f0a06df531d53">initialise</a> (<span class="keywordtype">int</span> num_threads) {</div><div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;       works.push_back( std::move(boost::asio::io_service::work(ioService)));</div><div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;       <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> nthr= 0; nthr &lt; num_threads; nthr++){</div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;         threadpool.create_thread(</div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;            boost::bind(&amp;boost::asio::io_service::run, &amp;ioService)</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;         );</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;       }</div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;    };</div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;  </div><div class="line"><a name="l00054"></a><span class="lineno"><a class="line" href="classgrppi_1_1thread__pool.html#ae82267412d6b212552dbe642f55b7d43">   54</a></span>&#160;    <a class="code" href="classgrppi_1_1thread__pool.html#ae82267412d6b212552dbe642f55b7d43">~thread_pool</a>(){</div><div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;        ioService.stop();</div><div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;        threadpool.join_all();</div><div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;     };</div><div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;</div><div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;};</div><div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;</div><div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;}</div><div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;</div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;<span class="preprocessor">#endif</span></div><div class="ttc" id="namespacegrppi_html"><div class="ttname"><a href="namespacegrppi.html">grppi</a></div><div class="ttdef"><b>Definition:</b> callable_traits.h:24</div></div>
<div class="ttc" id="classgrppi_1_1thread__pool_html_a0b4a992459c09e7cc72f0a06df531d53"><div class="ttname"><a href="classgrppi_1_1thread__pool.html#a0b4a992459c09e7cc72f0a06df531d53">grppi::thread_pool::initialise</a></div><div class="ttdeci">void initialise(int num_threads)</div><div class="ttdef"><b>Definition:</b> pool.h:45</div></div>
<div class="ttc" id="classgrppi_1_1thread__pool_html_a81730d72e5971e321618386e560f1cd1"><div class="ttname"><a href="classgrppi_1_1thread__pool.html#a81730d72e5971e321618386e560f1cd1">grppi::thread_pool::create_task</a></div><div class="ttdeci">void create_task(T task)</div><div class="ttdef"><b>Definition:</b> pool.h:43</div></div>
<div class="ttc" id="classgrppi_1_1thread__pool_html_ae82267412d6b212552dbe642f55b7d43"><div class="ttname"><a href="classgrppi_1_1thread__pool.html#ae82267412d6b212552dbe642f55b7d43">grppi::thread_pool::~thread_pool</a></div><div class="ttdeci">~thread_pool()</div><div class="ttdef"><b>Definition:</b> pool.h:54</div></div>
<div class="ttc" id="classgrppi_1_1thread__pool_html_a53583fb17ac9cbbe4c5d81507265c145"><div class="ttname"><a href="classgrppi_1_1thread__pool.html#a53583fb17ac9cbbe4c5d81507265c145">grppi::thread_pool::thread_pool</a></div><div class="ttdeci">thread_pool()</div><div class="ttdef"><b>Definition:</b> pool.h:39</div></div>
<div class="ttc" id="classgrppi_1_1thread__pool_html"><div class="ttname"><a href="classgrppi_1_1thread__pool.html">grppi::thread_pool</a></div><div class="ttdef"><b>Definition:</b> pool.h:30</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.11
</small></address>
</body>
</html>
